home *** CD-ROM | disk | FTP | other *** search
- unit Ccscreen;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, ExtCtrls;
-
- const
-
- CCSaverJumpIncrement = 5; { This controls the speed of bitmap motion }
- { 1=crawl; 10+=jerky }
- type
- TCCScreenSaverForm = class(TForm)
- CCSSTImer: TTimer;
- procedure FormShow(Sender: TObject);
- procedure FormHide(Sender: TObject);
- procedure FormActivate(Sender: TObject);
- procedure CCSSTImerTimer(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- CurrentXLocation , { Position of the bitmap onscreen X }
- CurrentYLocation , { Position of the bitmap onscreen Y }
- CurrentDeltaX , { Amount to move left or right (-x) }
- CurrentDeltaY : Integer; { Amount to move up or down (-y) }
- TheDisplayBitmap : TBitmap; { Bitmap to use in the saver }
- TheSavedBackgroundBitmap , { Portion of BgBMP saved each time }
- TheWorkSpaceBitmap : TBitmap; { Offscreen workspace to avoid flikr}
- procedure ShutDownTheScreenSaver( var Msg : TMsg;
- var Handled : boolean ); { This handles checking to shut down }
- end;
-
- var
- CCScreenSaverForm: TCCScreenSaverForm;
- StartingMousePosition : TPoint; { This is where the mouse is when saver invoked }
- implementation
-
- {$R *.DFM}
-
- uses
- Ccsavrsu, { This is the setup unit; used if /c in paramaters at startup }
- CCErrors; { This is the custom error handling unit. }
-
- { This procedure is used to check any incoming message for a keyboard/mouse }
- { event; if one is found the saver is deactivated; otherwise the message is }
- { passed through. }
- procedure TCCScreenSaverForm.ShutDownTheScreenSaver(var Msg : TMsg; var Handled : boolean);
- var
- Finished : boolean; { This is a flag to tell whether to quit }
- begin
- { Assume the saver will continue to run }
- Finished := false;
- { If the mouse is moved more than 5 pixels from start then kill the saver }
- if Msg.message = WM_MOUSEMOVE then
- begin
- { Obtain an absolute value of difference between X and Y positions }
- { which are in the Lo and Hi parameter words respectively. }
- if ( Abs( LoWord( Msg.lParam ) - StartingMousePosition.X ) > 5 ) then
- Finished := true;
- if ( Abs( HiWord( Msg.lParam ) - StartingMousePosition.Y ) > 5 ) then
- Finished := true;
- end
- else
- begin
- { Check for a plethora of possible keyboard/mousebutton event messages }
- case Msg.message of
- WM_ACTIVATE : Finished := true;
- WM_ACTIVATEAPP : Finished := true;
- WM_KEYDOWN : Finished := true;
- WM_KEYUP : Finished := true;
- WM_LBUTTONDOWN : Finished := true;
- WM_MBUTTONDOWN : Finished := true;
- WM_NCACTIVATE : Finished := true;
- WM_RBUTTONDOWN : Finished := true;
- WM_SYSKEYDOWN : Finished := true;
- WM_SYSKEYUP : Finished := true;
- end;
- end;
- { If any appropriate message found kill the screen saver! }
- if Finished then Close;
- end;
-
- { This code is written by Delphi; put in the calls to set up the starting }
- { mouse position, the speed of the bitmap motion, and the starting bitmap }
- { coordinates chosen randomly. Also set up the message hook and hide the }
- { cursor for neatness sake. Finally, get the necessary canvases to show }
- { the moving bitmap smoothly. Last, load the bitmap to use as an image! }
- procedure TCCScreenSaverForm.FormShow(Sender: TObject);
- begin
- { Seed the random number generator }
- Randomize;
- { Use an API call to get the position of the mouse cursor at startup }
- GetCursorPos( StartingMousePosition );
- { Set the speed to move the bitmaap between 910 and 100 ms }
- CCSSTimer.Interval := 305 - ( SetupDialog.TheImageSpeed * 3 );
- { Start the timer }
- CCSSTimer.Enabled := true;
- { Hook the message processing call into the Application object }
- Application.OnMessage := ShutDownTheScreenSaver;
- { Use an API call to hide the cursor for neatness! }
- ShowCursor( false );
- TheDisplayBitmap := TBitmap.Create;
- { Load the image file name and signal an error if unable to }
- {$I+}
- try
- TheDisplayBitmap.LoadFromFile( SetupDialog.TheImageFilename );
- except
- { abort on any io error at all! }
- on E:EInOutError do
- begin
- { Signal the error condition }
- ErrorDialog( SetupDialog.TheImageFileName + ' ' +
- GetIOErrorMessage( E.ErrorCode ) + '! Aborting Saver!' );
- { abort the saver }
- exit;
- end;
- end;
- { Get a Position where the image can move at least a little }
- CurrentXLocation := Random(( Screen.Width - ( TheDisplayBitmap.Width * 2 ))) + 1;
- CurrentYLocation := Random(( Screen.Height - ( TheDisplayBitmap.Height * 2 ))) + 1;
- { Start out moving right and down }
- CurrentDeltaX := CCSaverJumpIncrement;
- CurrentDeltaY := CCSaverJumpIncrement;
- { Create the two bitmaps to handle smooth animation }
- TheSavedBackgroundBitmap := TBitmap.Create;
- TheWorkspaceBitmap := TBitmap.Create;
- { Set the bitmaps to appropriate height and width values }
- TheWorkspaceBitmap.Width := Screen.Width;
- TheWorkSpaceBitmap.Height := Screen.Height;
- TheSavedBackgroundBitmap.Width := TheDisplayBitmap.Width;
- TheSavedBackgroundBitmap.Height := TheDisplayBitmap.Height;
- { Blank out the workspace's canvas }
- with TheWorkspaceBitmap.Canvas do
- begin
- brush.color := clblack;
- pen.color := clblack;
- Brush.style := bsSolid;
- Rectangle( 0 , 0 , Screen.Width , Screen.Height );
- end;
- { Put the background behind the saved bitmap into the save buffer }
- TheSavedBackgroundBitmap.Canvas.Copyrect( Rect( 0 , 0 , TheDisplayBitmap.Width ,
- TheDisplayBitmap.Height ) , TheWorkSpacebitmap.canvas , Rect( CurrentXLocation ,
- CurrentYLocation , CurrentXLocation + TheDisplayBitmap.Width , CurrentYLocation +
- TheDisplayBitmap.Height ));
- { Put the image in the workspace }
- TheWorkspaceBitmap.Canvas.Copyrect( Rect( CurrentXLocation , CurrentYLocation ,
- CurrentXLocation + TheDisplayBitmap.Width , CurrentYLocation + TheDisplayBitmap.Height ) ,
- TheDisplayBitmap.Canvas , Rect( 0 , 0 , TheDisplayBitmap.Width , TheDisplayBitmap.Height ));
- { And blas it to the screen to start off }
- CCScreenSaverForm.Canvas.CopyRect( Rect( CurrentXLocation , CurrentYLocation ,
- CurrentXLocation + TheDisplayBitmap.Width , CurrentYLocation + TheDisplayBitmap.Height ) ,
- TheWorkspaceBitmap.Canvas , Rect( CurrentXLocation , CurrentYLocation , CurrentXLocation +
- TheDisplayBitmap.Width , CurrentYLocation + TheDisplayBitmap.Height ));
- end;
-
- { Delphi wrote this; put in calls to unhook the message handler and turn off }
- { the timer and show the cursor again. Then free all the bitmaps. }
- procedure TCCScreenSaverForm.FormHide(Sender: TObject);
- begin
- { Set the message handler hook to nil to abort message processing }
- Application.OnMessage := nil;
- { Turn off the timer until needed again }
- CCSSTimer.Enabled := false;
- { Make an API call to turn on the cursor again }
- ShowCursor( true );
- { Free all the bitmaps till next time }
- TheDisplayBitmap.Free;
- TheSavedBackGroundBitmap.Free;
- TheWorkspaceBitmap.Free;
- end;
-
- { Delphi wrote this; use it to maximize the window to the whole screen }
- procedure TCCScreenSaverForm.FormActivate(Sender: TObject);
- begin
- { Maximize to the whole screen }
- WindowState := wsMaximized;
- end;
-
- { Delphi wrote this; use it to actually move the bitmap around to clear }
- { the screen during the operation of the screen saver form }
- procedure TCCScreenSaverForm.CCSSTImerTimer(Sender: TObject);
- var OldXLocation ,
- OldYLocation : Integer;
- ComputedXLocation ,
- ComputedYLocation : Integer;
- begin
- { Move the bitmap along the chosen coordinate until it hits an edge }
- with TheWorkSpaceBitmap.Canvas do
- begin
- {Erase bitmap by copying in saved background}
- CopyMode := cmSrcCopy;
- CopyRect( Rect( CurrentXLocation , CurrentYLocation , CurrentXLocation +
- TheDisplayBitmap.Width, CurrentYLocation + TheDisplayBitmap.Height ) ,
- TheSavedBackgroundBitmap.Canvas, Rect( 0 , 0 , TheDisplayBitmap.Width ,
- TheDisplayBitmap.Height ));
- OldXLocation := CurrentXLocation;
- OldYLocation := CurrentYLocation;
- { set new bitmap position}
- { first increment the position by current counters }
- CurrentXLocation := CurrentXLocation + CurrentDeltaX;
- CurrentYLocation := CurrentYLocation + CurrentDeltaY;
- { If at the right edge, move left and randomly set the up/down move }
- if ( CurrentXLocation + TheDisplayBitmap.Width ) >= Screen.Width then
- begin
- CurrentDeltaX := -CCSaverJumpIncrement;
- if Random( 10 ) > 7 then { 3 out of ten times go either reverse or flat }
- case CurrentDeltaY of
- -CCSaverJumpIncrement : CurrentDeltaY := 0;
- 0 : CurrentDeltaY := CCSaverJumpIncrement;
- CCSaverJumpIncrement : CurrentDeltaY := -CCSaverJumpIncrement;
- end;
- end;
- { If at the bottom, move up and randomly set the right/left move }
- if ( CurrentYLocation + TheDisplayBitmap.Height ) >= Screen.Height then
- begin
- CurrentDeltaY := -CCSaverJumpIncrement;
- if Random( 10 ) > 7 then { 3 out of ten times go either reverse or flat }
- case CurrentDeltaX of
- -CCSaverJumpIncrement : CurrentDeltaX := 0;
- 0 : CurrentDeltaX := CCSaverJumpIncrement;
- CCSaverJumpIncrement : CurrentDeltaX := -CCSaverJumpIncrement;
- end;
- end;
- { If at the left move right and randomly set the up/down move }
- if CurrentXLocation <= 1 then
- begin
- CurrentDeltaX := CCSaverJumpIncrement;
- if Random( 10 ) > 7 then { 3 out of ten times go either reverse or flat }
- case CurrentDeltaY of
- -CCSaverJumpIncrement : CurrentDeltaY := 0;
- 0 : CurrentDeltaY := CCSaverJumpIncrement;
- CCSaverJumpIncrement : CurrentDeltaY := -CCSaverJumpIncrement;
- end;
- end;
- { If at the top move down and randomly set the right/left move }
- if CurrentYLocation <= 1 then
- begin
- CurrentDeltaY := CCSaverJumpIncrement;
- if Random( 10 ) > 7 then { 3 out of ten times go either reverse or flat }
- case CurrentDeltaX of
- -CCSaverJumpIncrement : CurrentDeltaX := 0;
- 0 : CurrentDeltaX := CCSaverJumpIncrement;
- CCSaverJumpIncrement : CurrentDeltaX := -CCSaverJumpIncrement;
- end;
- end;
- {save background at new bitmap position}
- TheSavedBackgroundBitmap.Canvas.CopyRect( Rect( 0 , 0 , TheDisplayBitmap.Width ,
- TheDisplayBitmap.Height), TheWorkSpaceBitmap.Canvas, Rect( CurrentXLocation ,
- CurrentYLocation , CurrentXLocation + TheDisplayBitmap.Width, CurrentYLocation +
- TheDisplayBitmap.Height ));
- {copy the bitmap into place}
- CopyRect( Rect( CurrentXLocation , CurrentYLocation , CurrentXLocation +
- TheDisplayBitmap.Width, CurrentYLocation + TheDisplayBitmap.Height ) ,
- TheDisplayBitmap.Canvas, Rect( 0 , 0 , TheDisplayBitmap.Width ,
- TheDisplayBitmap.Height ));
- end;
- {Now blast the finished image to the screen!}
- with CCScreenSaverForm.Canvas do
- begin
- if CurrentXLocation < OldXLocation then
- ComputedXLocation := CurrentXLocation else
- ComputedXLocation := OldXLocation;
- if CurrentYLocation < OldYLocation then
- ComputedYLocation := CurrentYLocation else
- ComputedYLocation := OldYLocation;
- CopyRect( Rect( ComputedXLocation , ComputedYLocation , ComputedXLocation +
- TheDisplayBitmap.Width + CCSaverJumpIncrement + 2, ComputedYLocation +
- TheDisplayBitmap.Height + CCSaverJumpIncrement + 2 ) , TheWorkSpaceBitmap.Canvas,
- Rect( ComputedXLocation , ComputedYLocation ,
- ComputedXLocation + TheDisplayBitmap.Width + CCSaverJumpIncrement + 2,
- ComputedYLocation + TheDisplayBitmap.Height + CCSaverJumpIncrement + 2));
- end;
- end;
-
- end.
-